﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _0912092_BaiTap01_SymbolsReport
{
    public abstract class DemTanSoNhomKyTu
    {
        protected DemTanSoNhomKyTu nextProcess;

        public DemTanSoNhomKyTu NextProcess
        {
            get { return nextProcess; }
            set {
                if (value != null)
                {
                    nextProcess = value;
                }                
            }
        }


        protected String textInput;
        protected List<NhomKyTu> danhSachNhomKyTu;


        public void Run(String Input)
        {
            DieuChinhChuoiInput(Input);
            KhoiTaoDanhSachNhomKyTu();
            DemTanSoXuatHienCuaNhomKyTu();
            SapXepDanhSachNhomKyTuTheoTanSo();
            HienThiDanhSachNhomKyTu();
            if (nextProcess != null)
            {
                nextProcess.Run(Input);
            }

        }

        protected void HienThiDanhSachNhomKyTu()
        {
            HienThiTitle();

            int length=0;
            if (danhSachNhomKyTu != null)
            {
                length = danhSachNhomKyTu.Count;
                if (length >= 10)
                {
                    length = 10;
                }
            }            
            for (int i = 0; i < length; i++)
            {
                NhomKyTu nkt = danhSachNhomKyTu[i];
                Console.WriteLine(nkt.ChuoiKyTu+":"+nkt.TanSo);
            }

        }

        protected virtual void HienThiTitle()
        {            
        }

        protected void SapXepDanhSachNhomKyTuTheoTanSo()
        {
            int nNhomKyTu=danhSachNhomKyTu.Count;
            for (int i = 0; i < nNhomKyTu; i++)
            {
                for (int j = i + 1; j < nNhomKyTu - 1; j++)
                {
                    if (danhSachNhomKyTu[i].TanSo < danhSachNhomKyTu[j].TanSo)
                    {
                        danhSachNhomKyTu[i].SwapTo(danhSachNhomKyTu[j]);
                    }
                }
            }
        }

        protected virtual void DemTanSoXuatHienCuaNhomKyTu()
        {
        }

        protected abstract void KhoiTaoDanhSachNhomKyTu();
        protected void DieuChinhChuoiInput(string Input)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append(Input.ToUpper());

            int length = strBuilder.Length;
            for (int i = 0; i < length; i++)
            {
                if (strBuilder[i] < 65 || strBuilder[i] > 91)
                {
                    strBuilder[i] = ' ';
                }
            }
            char[] trimCharacters=new char[]{' '};
            textInput = strBuilder.ToString().Trim(trimCharacters);
        }
    }
}
